package org.jtsmix.editor;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MixEditorImpl extends MixReaderImpl implements MixReader, MixEditor {

    private static final int FILE_BUFFER_SIZE = 4096;

    @Override
    public void addFile(String fileName) throws FileNotFoundException {

        File file = new File(fileName);

        if (mixfile == null) {
            return;
        }

        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream os = new ByteArrayOutputStream();


        byte buffer[] = new byte[FILE_BUFFER_SIZE];
        try {
            int i = 0;
            while ((i = fis.read(buffer)) != -1) {
                os.write(buffer, 0, i);
            }
            os.flush();
            fis.close();
            mixfile.write(file.getName(), os.toByteArray());
            os.close();

        } catch (IOException ex) {
            Logger.getLogger(MixEditorImpl.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    @Override
    public void createMix(String fileName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void removeFile(String fileName) {
        try {
            mixfile.remove(fileName);
        } catch (IOException ex) {
            Logger.getLogger(MixEditorImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}